//
//  TabBarController.swift
//  fho
//
//  Created by 郭颢源 on 2021/3/19.
//

import UIKit

class TabBarController: UITabBarController {
    fileprivate func setLayer() {
        let S_Width = UIScreen.main.bounds.width
        let bezier = UIBezierPath()
        bezier.move(to: CGPoint.zero)
        bezier.addLine(to: CGPoint(x: S_Width * 0.5 - 27, y: 0))
        
        bezier.append(UIBezierPath(arcCenter: CGPoint(x: S_Width * 0.5, y: 15), radius: 30, startAngle: 0, endAngle: CGFloat.pi * -1, clockwise: false))
        
        bezier.move(to: CGPoint(x: S_Width * 0.5 + 27, y: 0))
        bezier.addLine(to: CGPoint(x: S_Width, y: 0))
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = bezier.cgPath
        shapeLayer.lineWidth = 1
        shapeLayer.fillColor = #colorLiteral(red: 0.9764705882, green: 0.9764705882, blue: 0.9764705882, alpha: 1).cgColor
        shapeLayer.strokeColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1).cgColor
        tabBar.backgroundColor = #colorLiteral(red: 0.9764705882, green: 0.9764705882, blue: 0.9764705882, alpha: 1)
        tabBar.layer.insertSublayer(shapeLayer, at: 0)
        
        tabBar.shadowImage = UIImage()
        tabBar.backgroundImage = UIImage()
    }
    
    override func viewDidLoad() {
        setLayer()
        super.viewDidLoad()
        self.tabBar.barStyle = .default //tabbar 颜色
        self.tabBar.isTranslucent = false //设置tabBar是否半透明(毛玻璃效果)
        self.tabBar.tintColor = .systemBlue //选中字体颜色
        
        //设置tabBarItem的title，以及点击和不点击状态图片
        
        //去掉tabar 上边线
        self.tabBar.backgroundImage = UIImage()
        self.tabBar.shadowImage = UIImage()
        
        //设置tabBarItem的title的字体颜色和字大小及字体样式
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor : UIColor.red], for: .selected)
        //        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 10), NSAttributedString.Key.foregroundColor : UIColor.green], for: .normal)
        //        self.edgesForExtendedLayout = UIRectEdgeNone;
        //tabBar 底部工具栏背景颜色 (以下两个都行)
        
        addNormalTabbar()
        //默认选中
        selectedIndex = 2
    }
    // MARK: -FirstNormalTabba 第一种常用的tabbar
    /**
     优点可以拿到单独子控制器 做相应的设置
     */
    func addNormalTabbar() {
        //这里针对updateVc 改变其背景色
        
        setupOneChildViewController(title: "首页", image: "iconFirst", seletedImage: "iconFirst", controller: FirstController())
        setupOneChildViewController(title: "查单", image: "iconSecond", seletedImage: "iconSecond", controller: JSExpressQueryController())
        setupOneChildViewController(title: "发货", image: "iconThird", seletedImage: "iconThird", controller: ThirdController())
        setupOneChildViewController(title: "订单", image: "iconFour", seletedImage: "iconFour", controller: FourController())
        setupOneChildViewController(title: "我的", image: "iconFive", seletedImage: "iconFive", controller: FiveController())
    }
    fileprivate func  setupOneChildViewController(title: String,image: String,seletedImage: String,controller: UIViewController){
        
        //                 homeVC.tabBarItem.setTitleTextAttributes([.foregroundColor:UIColor.hexColor(0x333333)], for: .selected)
        //        [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor : UIColor.red]

        //        controller.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font:UIFont.systemFont(ofSize: 12)], for: .normal)//仅在normal 下生效
        
        controller.title = title
        //这里设置背景色 是每一个vc设置的都一样
        controller.view.backgroundColor = .white
        controller.tabBarItem.title = title
        controller.tabBarItem.image = UIImage(named: image)?.withRenderingMode(.alwaysOriginal)
        controller.tabBarItem.selectedImage = UIImage(named: seletedImage)?.withRenderingMode(.alwaysOriginal)
        
        if title.hasPrefix("发货") {
            controller.tabBarItem.imageInsets = UIEdgeInsets(top: -30, left: 0, bottom: 15, right: 0)
        }else{
            controller.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 0)
        }
        
        let naviController = NavigationController(rootViewController: controller)
        addChild(naviController)
        
    }
    
}
